<?php

declare(strict_types=1);

namespace App\Crontab;

use App\Constants\OrderPayStatus;
use App\Constants\OrderStatus;
use App\Model\Order;
use Carbon\Carbon;
use Hyperf\Collection\Collection;

class OrderInvalidCrontab
{
    public function execute(): string
    {
        // 查询待支付订单
        Order::query()
            ->where('pay_lapse_at', '<=', Carbon::now())
            ->where('pay_status', OrderPayStatus::NONE)
            ->where('status', OrderStatus::WAIT_PAY)
            ->chunk(1000, function (Collection $orders) {
                /** @var Order $order */
                foreach ($orders as $order) {
                    $order->status = OrderStatus::INVALID;
                    $order->save();
                }
            });

        return 'success';
    }
}
